package com.yifi.module.jwflow.controller;

import cn.hutool.core.util.StrUtil;
import com.jfinal.core.Path;
import com.yifi.common.base.BaseController;
import com.yifi.common.utils.ToolSession;

/**
 * 工作流任务
 */
@Path(value="/jwflow_task",viewPath = "/jwflow/")
public class jwflow_task_controller extends BaseController {
    public void index()
    {
        //范围值
        String range=getPara("range","");
        setAttr("range",range);
        //工作流代码
        String code=getPara("code","");
        setAttr("code",code);
        render("jwflow_task.html");
    }

    public void list()
    {
        String user=ToolSession.getUser(this).getCcode();

        String orderByColumn=getPara("orderByColumn","update_time");
        String isAsc=getPara("isAsc","desc");

        String code=getPara("code","all");			//工作流代码

        String range=getPara("range","");		//外部参数，工作流处理

        String status=getPara("status","1");		//处理范围，未处理完成 / 已处理完成

        String wfflag=getPara("wfflag","1");	//我发起的/我处理的/全部我参与的

        String wf_id=getPara("wf_id","all");
        String task_name=getPara("task_name","");
        String task_desc=getPara("task_desc","");

        String sdate=getPara("sdate","");
        String edate=getPara("edate","");
        String sqlSelect="select t1.*,t2.detail_url,t2.app_url,t3.detail_url as detail_url_all,t2.name as node_name,t2.shape as node_type ";
        String sqlExcept = " from jw_wf_task t1,jw_wf_node t2,jw_wf_code t3 "
                + " where t1.wf_id=t2.wf_id and t1.node_id=t2.id and t1.wf_id=t3.id ";

        if(!status.equals("all"))
        {
            sqlExcept+=" and t1.status ='"+status+"' ";
        }
        //sqlExcept+=" and t1.status in ('0','1') ";


        if(StrUtil.isNotEmpty(task_name))
        {
            sqlExcept+=" and t1.task_name like '%"+task_name+"%'";
        }
        if(!wf_id.equals("all"))
        {
            sqlExcept+=" and t1.wf_id = '"+wf_id+"'";
        }
        if(StrUtil.isNotEmpty(task_desc))
        {
            sqlExcept+=" and t1.task_desc like '%"+task_desc+"%'";
        }

        if(range.equals("all"))
        {

        }
        else if(range.equals("start"))      //开始的
        {
            sqlExcept+=" and t1.create_by = '"+user+"'";
        }
        else if(range.equals("deal"))       //需要处理的
        {
            //sqlExcept+=" and t1.approver like '%;"+user+";%'";
            sqlExcept+=" and t1.id in (select task_id from jw_wf_task_handler where task_id=t1.id and wf_id=t3.id and user_id ='"+user+"')";
        }

        if(wfflag.equals("1"))          //我发起的
        {
            sqlExcept+=" and (t1.create_by = '"+user+"' ) ";
        }
        if(wfflag.equals("2"))          //我参与的
        {
            sqlExcept+=" and t1.id in (select task_id from jw_wf_process where create_by = '"+user+"') ";
        }

        if(wfflag.equals("all"))           //全部的
        {
            sqlExcept+=" and (t1.create_by = '"+user+"' or t1.id in (select task_id from jw_wf_process where create_by = '"+user+"')) ";
        }

        if(StrUtil.isNotEmpty(sdate))
        {
            sqlExcept+=" and date_format(t1.create_time,'%Y-%m-%d') >='"+sdate+"' ";
        }
        if(StrUtil.isNotEmpty(edate))
        {
            sqlExcept+=" and date_format(t1.create_time,'%Y-%m-%d') <='"+edate+"'";
        }

        sqlExcept+=" order by "+orderByColumn+" "+isAsc+" ";
        renderJson(getDataTable(sqlSelect, sqlExcept));
    }
}
